/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginViewFrame.java
 *
 * Created on Mar 15, 2011, 10:03:45 AM
 */

package vn.support.ui.frame;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import vn.support.system.SystemProperties;

/**
 *
 * @author nttuyen
 */
public final class LoginViewFrame extends AbstractLoginView {
    /** Creates new form LoginViewFrame */
    public LoginViewFrame() {
        initComponents();
        this.init();
        this.refresh();
    }

    protected void init() {
        int width = 480;
        int height = 410;
        this.setSize(width, height);
        this.setResizable(false);
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        this.setLocation((screenWidth - width) / 2, (screenHeight - height) / 2);
    }
    @Override
    public void refresh() {
        this.setTitle(bundle("ui.login.frame.title"));
        this.jLabelMemberLogin.setText(bundle("ui.login.label.member.login"));
        this.jLabelLoginDescription.setText(bundle("ui.login.label.description"));
        this.jlabelUserName.setText(bundle("ui.login.label.username"));
        this.jlabelPassword.setText(bundle("ui.login.label.password"));
        this.jcheckboxRemeber.setText(bundle("ui.login.label.checkbox.remember"));
        this.jbuttonLogin.setText(bundle("ui.login.button.login"));
        this.jbuttonCancel.setText(bundle("ui.login.button.cancel"));
        
        this.jMenuFile.setText(SystemProperties.getValue("ui.login.menu.file"));
        this.jMenuItemExit.setText(SystemProperties.getValue("ui.login.menu.file.exit"));
        this.jMenuSetting.setText(SystemProperties.getValue("ui.login.menu.setting"));
        this.jMenuItemServer.setText(SystemProperties.getValue("ui.login.menu.setting.server"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelContainer = new javax.swing.JPanel();
        jPanelLoginInfo = new javax.swing.JPanel();
        jlabelUserName = new javax.swing.JLabel();
        jTextFieldUserName = new javax.swing.JTextField();
        jlabelPassword = new javax.swing.JLabel();
        jbuttonLogin = new javax.swing.JButton();
        jbuttonCancel = new javax.swing.JButton();
        jcheckboxRemeber = new javax.swing.JCheckBox();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jPanelLoginDescription = new javax.swing.JPanel();
        jLabelMemberLogin = new javax.swing.JLabel();
        jLabelLoginDescription = new javax.swing.JLabel();
        jLabelIcon = new javax.swing.JLabel();
        jMenuBar = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemExit = new javax.swing.JMenuItem();
        jMenuSetting = new javax.swing.JMenu();
        jMenuItemServer = new javax.swing.JMenuItem();

        setTitle("Login");

        jPanelContainer.setBackground(new java.awt.Color(176, 203, 246));

        jPanelLoginInfo.setBackground(new java.awt.Color(176, 203, 246));

        jlabelUserName.setText("Username");

        jlabelPassword.setText("Password");

        jbuttonLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/agt_action_success.png"))); // NOI18N
        jbuttonLogin.setText("Login");
        jbuttonLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbuttonLoginActionPerformed(evt);
            }
        });

        jbuttonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/33.png"))); // NOI18N
        jbuttonCancel.setText("Cancel");
        jbuttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbuttonCancelActionPerformed(evt);
            }
        });

        jcheckboxRemeber.setBackground(new java.awt.Color(176, 203, 246));
        jcheckboxRemeber.setText("Remeber Me");

        jPasswordFieldPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordFieldPasswordKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanelLoginInfoLayout = new javax.swing.GroupLayout(jPanelLoginInfo);
        jPanelLoginInfo.setLayout(jPanelLoginInfoLayout);
        jPanelLoginInfoLayout.setHorizontalGroup(
            jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLoginInfoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jcheckboxRemeber)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelLoginInfoLayout.createSequentialGroup()
                        .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlabelPassword)
                            .addComponent(jlabelUserName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                            .addComponent(jTextFieldUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)))
                    .addGroup(jPanelLoginInfoLayout.createSequentialGroup()
                        .addComponent(jbuttonLogin)
                        .addGap(18, 18, 18)
                        .addComponent(jbuttonCancel)))
                .addContainerGap())
        );
        jPanelLoginInfoLayout.setVerticalGroup(
            jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLoginInfoLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlabelUserName)
                    .addComponent(jTextFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlabelPassword)
                    .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcheckboxRemeber)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(jPanelLoginInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbuttonLogin)
                    .addComponent(jbuttonCancel))
                .addContainerGap())
        );

        jPanelLoginDescription.setBackground(new java.awt.Color(176, 203, 246));
        jPanelLoginDescription.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabelMemberLogin.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabelMemberLogin.setText("Member Login");

        jLabelLoginDescription.setText("Enter your username and password");

        javax.swing.GroupLayout jPanelLoginDescriptionLayout = new javax.swing.GroupLayout(jPanelLoginDescription);
        jPanelLoginDescription.setLayout(jPanelLoginDescriptionLayout);
        jPanelLoginDescriptionLayout.setHorizontalGroup(
            jPanelLoginDescriptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelLoginDescriptionLayout.createSequentialGroup()
                .addGroup(jPanelLoginDescriptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelLoginDescriptionLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelLoginDescription))
                    .addGroup(jPanelLoginDescriptionLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabelMemberLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanelLoginDescriptionLayout.setVerticalGroup(
            jPanelLoginDescriptionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelLoginDescriptionLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelMemberLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addComponent(jLabelLoginDescription)
                .addContainerGap())
        );

        jLabelIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/User-Group-icon1.png"))); // NOI18N

        javax.swing.GroupLayout jPanelContainerLayout = new javax.swing.GroupLayout(jPanelContainer);
        jPanelContainer.setLayout(jPanelContainerLayout);
        jPanelContainerLayout.setHorizontalGroup(
            jPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelContainerLayout.createSequentialGroup()
                .addGroup(jPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelContainerLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jPanelLoginDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelIcon))
                    .addGroup(jPanelContainerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelLoginInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelContainerLayout.setVerticalGroup(
            jPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelContainerLayout.createSequentialGroup()
                .addGroup(jPanelContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelContainerLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanelLoginDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29))
                    .addGroup(jPanelContainerLayout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(jLabelIcon)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jPanelLoginInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        getContentPane().add(jPanelContainer, java.awt.BorderLayout.PAGE_START);

        jMenuFile.setText("File");

        jMenuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.ALT_MASK));
        jMenuItemExit.setText("Exit");
        jMenuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemExitActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemExit);

        jMenuBar.add(jMenuFile);

        jMenuSetting.setText("Setting");

        jMenuItemServer.setText("Server");
        jMenuItemServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemServerActionPerformed(evt);
            }
        });
        jMenuSetting.add(jMenuItemServer);

        jMenuBar.add(jMenuSetting);

        setJMenuBar(jMenuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbuttonLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbuttonLoginActionPerformed
        final String username = this.jTextFieldUserName.getText().trim();
        final String password = new String(this.jPasswordFieldPassword.getPassword()).trim();
        login(username, password);
    }//GEN-LAST:event_jbuttonLoginActionPerformed

    private void jbuttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbuttonCancelActionPerformed
        if(this.processor != null) {
            this.processor.disable();
        }
        this.active();
    }//GEN-LAST:event_jbuttonCancelActionPerformed

    private void jPasswordFieldPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldPasswordKeyPressed
        int keycode = evt.getKeyCode();
        if(keycode == 10 || keycode == 13) {
            this.jbuttonLogin.setEnabled(false);
            this.jbuttonLoginActionPerformed(null);
        }
    }//GEN-LAST:event_jPasswordFieldPasswordKeyPressed

    private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed
        ((JFrame)SystemProperties.get(SystemProperties.MAIN_FRAME)).dispose();
        System.exit(0);
    }//GEN-LAST:event_jMenuItemExitActionPerformed

    private void jMenuItemServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemServerActionPerformed
        String url = SystemProperties.get(SystemProperties.URL);
        url = url.replace("/winapp/index.php", "");
        url = JOptionPane.showInputDialog(SystemProperties.getValue("ui.login.input.server.message"), url);
        if(url == null) {
            return;
        }
        url += "/winapp/index.php";
        SystemProperties.set(SystemProperties.URL, url);
        SystemProperties.save();
    }//GEN-LAST:event_jMenuItemServerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelIcon;
    private javax.swing.JLabel jLabelLoginDescription;
    private javax.swing.JLabel jLabelMemberLogin;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemExit;
    private javax.swing.JMenuItem jMenuItemServer;
    private javax.swing.JMenu jMenuSetting;
    private javax.swing.JPanel jPanelContainer;
    private javax.swing.JPanel jPanelLoginDescription;
    private javax.swing.JPanel jPanelLoginInfo;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JTextField jTextFieldUserName;
    private javax.swing.JButton jbuttonCancel;
    private javax.swing.JButton jbuttonLogin;
    private javax.swing.JCheckBox jcheckboxRemeber;
    private javax.swing.JLabel jlabelPassword;
    private javax.swing.JLabel jlabelUserName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void active() {
        this.processor = null;
        this.jbuttonLogin.setEnabled(true);
        this.jTextFieldUserName.setEnabled(true);
        this.jPasswordFieldPassword.setEnabled(true);
        this.jcheckboxRemeber.setEnabled(true);
    }

    @Override
    public void deactive() {
        this.jbuttonLogin.setEnabled(false);
        this.jTextFieldUserName.setEnabled(false);
        this.jPasswordFieldPassword.setEnabled(false);
        this.jcheckboxRemeber.setEnabled(false);
    }
}
